<template>
    <div class="categories-container">
        <!-- 測試：顯示完整 userInfo 響應式對象 -->
        <div style="color: red; font-size: 14px; margin-bottom: 8px;">
            userInfo: {{ JSON.stringify(userInfo.value) }}
        </div>
        <div class="categories-header">
            <div class="role-info">當前角色：{{ userInfo.value && userInfo.value.role }}</div>
            <button v-if="isAdmin" class="manage-btn" @click="goToAdmin">分類管理</button>
        </div>
        <div class="categories-list">
            <div
                    v-for="cat in categories"
                    :key="cat.id"
                    class="category-card"
                    @click="goToDetail(cat.id)"
            >
                <img :src="cat.image" class="category-img" />
                <div class="category-name">{{ cat.name }}</div>
                <div class="category-desc">{{ cat.description }}</div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()
const categories = ref([])
const userInfo = ref({})

const fetchCategories = async () => {
    const res = await axios.get('http://localhost:10010/flower/category/list')
    if (res.data && res.data.success) {
        categories.value = res.data.data.list
    }
}
const fetchUserInfo = async () => {
    const token = localStorage.getItem('token')
    if (token) {
        const res = await axios.get('http://localhost:10010/serviceUcenter/ucenter/getUserInfoForJwt', {
            headers: { Authorization: `Bearer ${token}` }
        })
        if (res.data && res.data.success) {
            userInfo.value = res.data.data.userInfo
        }
    }
}
const goToDetail = (id) => {
    router.push(`/categories/${id}`)
}
const isAdmin = computed(() => {
    return String(userInfo.value.role).toLowerCase().trim() === 'admin'
})
const goToAdmin = () => {
    router.push('/admin/categories')
}

onMounted(() => {
    fetchCategories()
    fetchUserInfo()
})
</script>

<style scoped>
.categories-container {
  max-width: 1100px;
  margin: 40px auto;
  padding: 24px;
}
.categories-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}
.role-info {
  color: #444;
  font-size: 16px;
}
.manage-btn {
  background: #409eff;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 8px 20px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.2s;
  box-shadow: 0 2px 8px rgba(64,158,255,0.08);
}
.manage-btn:hover {
  background: #337ecc;
}
.categories-list {
  display: flex;
  flex-wrap: wrap;
  gap: 32px;
  justify-content: center;
}
.category-card {
  width: 240px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.06);
  padding: 18px 18px 12px 18px;
  cursor: pointer;
  transition: box-shadow 0.2s;
  text-align: center;
}
.category-card:hover {
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
}
.category-img {
  width: 180px;
  height: 120px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 12px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.08);
}
.category-name {
  font-weight: bold;
  font-size: 18px;
  margin-bottom: 6px;
}
.category-desc {
  color: #888;
  font-size: 14px;
  min-height: 32px;
}
</style> 